package sky.tro.finders;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 22.09.13
 * Time: 14:17
 * To change this template use File | Settings | File Templates.
 */
public class AirportFinder implements Serializable, AbstractFinder {

    private String countryName = "";
    private String cityName = "";
    private String code = "";

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public boolean isEmpty() {
        return countryName==null && cityName==null && code.equals("");
    }

    public Criteria fillCriteria(Criteria criteria) {
        if (getCityName()!=null) {
            criteria.createAlias("city", "city")
                    .add(Restrictions.eq("city.name", getCityName()));
        }
        if (getCountryName()!=null) {
            if (getCityName()==null) criteria.createAlias("city", "city");
            criteria.createAlias("city.country", "country")
                    .add(Restrictions.eq("country.name", getCountryName()));
        }
        if (!getCode().equals("")) {
            criteria.add(Restrictions.eq("code", getCode()));
        }
        return criteria;
    }
}
